package com.example.albumphoto

import android.content.Context
import android.util.Log

class DataFactory {
    companion object {
        private val TAG = "DataFactory"
        val previewPhotosIdKey = "photosId"
        val previewTitleKey = "previewTitle"
        val EXTRA_PHOTOS_ARR = "EXTRA_PHOTOS_ARR"
        val EXTRA_POSITION = "EXTRA_POSITION"
        fun getNavDatas(context: Context): List<NavDataBean>{
            val list = mutableListOf<NavDataBean>()
            val names = context.resources.getStringArray(R.array.nav_name)
            val iconIds = context.resources.obtainTypedArray(R.array.nav_icon)
            val linkPhotoIds = context.resources.obtainTypedArray(R.array.preview_src)
            for (i in names.indices) {
                val bean = NavDataBean(
                    title = names[i],
                    imageResId = iconIds.getResourceId(i, 0),
                    linkPhotosId = linkPhotoIds.getResourceId(i, 0)
                )
                list.add(bean)
            }
            iconIds.recycle()
            linkPhotoIds.recycle()
            return list
        }

        fun getPreviewDatas(context: Context, arrayId: Int): List<PreViewDataBean>{
            val list = mutableListOf<PreViewDataBean>()
            val photoIds = context.resources.obtainTypedArray(arrayId)

            try {
                for (i in 0 until photoIds.length()) {
                    val bean = PreViewDataBean(
                        id = i,
                        imageResId = photoIds.getResourceId(i, 0),
                        title = "P" + (i + 1),
                        linkPhotosId = arrayId
                    )
                    Log.i(TAG, "imageResId = ${bean.imageResId}")
                    list.add(bean)
                }
            } finally {
                photoIds.recycle() // 必须回收
            }
            return list
        }

        fun getDetailDatas(context: Context, arrayId: Int): List<Int>{
            val list = mutableListOf<Int>()
            val photoIds = context.resources.obtainTypedArray(arrayId)
            try {
                for (i in 0 until photoIds.length()) {
                    val imageResId = photoIds.getResourceId(i, 0)
                    Log.i(TAG, "imageResId = ${imageResId}")
                    list.add(imageResId)
                }
            } finally {
                photoIds.recycle() // 必须回收
            }
            return list
        }
    }
}